package com.shketai.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.shketai.entity.Attence;
import com.shketai.entity.Info;
import com.shketai.entity.Student;
import com.shketai.entity.User;

public class StudentDao {

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public int checkPersonalNum(String personalNum){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Student where personalNum=?");
		query.setString(0, personalNum);
		List<Student> list = query.list();
		if(list.size() > 0){
			return 0;
		}else{
			return 1;
		}
	}
	/**
	 * 新增学生信息
	 * @param student
	 * @return
	 */
	public int add(Student student,int user_id){
	
		Session session = sessionFactory.getCurrentSession();
		User user = new User();
		user.setId(user_id);
		student.setUser(user);
		session.save(student);
		
		return 1;
	}
	public Student findByWorkId(int id) {
		Session session = sessionFactory.getCurrentSession();
		Student student = (Student) session.get(Student.class, id);
		return student;
	}

	/**
	 * 修改学生信息
	 * @param student
	 */
	public void update(Student student,int user_id) {
		Session session = sessionFactory.getCurrentSession();
		
		Query query = session.createQuery("from Student where id=:id");
		query.setInteger("id", student.getId());
		List<Student> list = query.list();
		Student oldUser = list.get(0);
		student.setId(oldUser.getId());
		User user = new User();
		user.setId(user_id);
		student.setUser(user);
		session.clear();
		session.update(student);
	}
	/**
	 * 修改学生备注
	 * @param student
	 * @param id
	 * @return
	 */
	public void updateFlag(int id,String flag){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Info set flag=:flag where id=:id");
		//引号id
		query.setInteger("id",id);
		query.setString("flag", flag);
		query.executeUpdate();
	}
	
	public int delete(Student student){
		Session session = sessionFactory.getCurrentSession();
		session.delete(student);
		return 1;
	}
	/**
	 * 查看个人信息
	 * @param id
	 * @return
	 */
	public List<Student> findById(int user_id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Student where user_id=?");
		query.setInteger(0, user_id);
		List<Student> list = query.list();
		if(list.size() > 0){
			return list;
		}else{
			return null;
		}
	}
	
	public List<Student> findById1(int id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Student where id=?");
		query.setInteger(0, id);
		List<Student> list = query.list();
		if(list.size() > 0){
			return list;
		}else{
			return null;
		}
	}
	
	/**
	 * 教师端 查看所有学生信息
	 * @param id
	 * @return
	 */
	public List<Student> find(int page){
		String hql = "from Student";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult((page-1)*10);
		query.setMaxResults(10);
		List<Student> list = query.list();
		return list;
	}
	public int findCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Student");
		Long total = (Long) query.uniqueResult();
		return total.intValue();
	}
	
	public List<Student> finddesc(int page){
		String hql = "from Student order by id desc";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult((page-1)*10);
		query.setMaxResults(10);
		List<Student> list = query.list();
		return list;
	}
	/**
	 * 根据姓名查询学生信息
	 * @param username
	 * @return
	 */
	public List<Student> queryByUsername(String stuName){
		Session session = sessionFactory.getCurrentSession();
		List<Student> list = session.createQuery("from Student where stuName like ? ").setString(0,"%"+stuName+"%").list();
		return list;
	}
	
	
}
